package com.yjd.merchant.api.order;

import com.yjd.comm.base.control.MerchantBaseControl;
import com.yjd.comm.base.enums.DbWREnums;
import com.yjd.comm.enums.OrderRefuseEnum;
import com.yjd.comm.enums.RefundOrderStatus;
import com.yjd.comm.fundservice.service.IFundChannelService;
import com.yjd.comm.order.model.OrderModel;
import com.yjd.comm.order.model.RefundOrderModel;
import com.yjd.comm.order.service.IOrderService;
import com.yjd.comm.util.DateUtil;
import com.yjd.comm.util.FrameUtil;
import com.yjd.comm.util.ServletUtil;
import com.yjd.comm.util.StringUtil;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * Created by yjd-wap on 2017/8/16.
 */

@Controller
@Scope("prototype")
@RequestMapping("/order/")
public class OrderControl extends MerchantBaseControl {

    @RequestMapping(value = "confirm", method = RequestMethod.POST)
    public void confirm(String order_no) throws Exception {
        if (StringUtil.isEmpty(order_no)) {
            this.errorMsg("参数错误");
            return;
        }
        OrderModel orderModel = orderService.getModelOne(FrameUtil.newHashMap("order_no", order_no), DbWREnums.READ);
        if (orderModel == null) {
            this.errorMsg("订单不存在");
            return;
        }

        /**
         * 如果orderModel.getFund_channel() > 0，说明使用的第三方资金，分两个异步过程：
         * 1、先调用第三方贷款申请
         * 2、等待异步回调，根据是否通过做相应处理：
         *  通过：生成还款报表、更新商户资金
         *  未通过：拒绝订单
         */
        if(orderModel.getFund_channel().intValue() > 0){
            this.fundChannelService.loanApply(super.getMerchantId(), orderModel.getOrder_id());
            this.successMsg("贷款申请处理中");
            return;
        }

        orderService.merchantConfirm(super.getMerchantId(), orderModel.getOrder_id());
        this.successMsg("订单确认成功");
    }

    /**
     * 商户取消订单
     * Created by yangchangyan on 2017/8/23.
     *
     * @param
     * @return
     * @throws
     */
    @RequestMapping(value = "refuse", method = RequestMethod.POST)
    public void refuse(String order_no, String refuse_code, String content) throws Exception {
        if (StringUtil.isEmpty(order_no)) {
            this.errorMsg("参数错误");
            return;
        }
        OrderModel orderModel = orderService.getModelOne(FrameUtil.newHashMap("order_no", order_no), DbWREnums.READ);
        if (orderModel == null) {
            this.errorMsg("订单不存在");
            return;
        }

        if (StringUtil.isEmpty(refuse_code)) {
            this.errorMsg("请选择拒绝码");
            return;
        }

        OrderRefuseEnum orderRefuseEnum = OrderRefuseEnum.parse(refuse_code);
        if (orderRefuseEnum == null) {
            this.errorMsg("您请求拒绝码不合法！");
            return;
        }

        if (OrderRefuseEnum.C.getCode().equals(orderRefuseEnum.getCode())) {
            if(StringUtil.isEmpty(content)){
                this.errorMsg("请输入拒绝原因！");
                return;
            }
        }

        orderService.merchantRefuse(super.getMerchantId(), orderModel.getOrder_id(), orderRefuseEnum, content);
        this.successMsg("订单拒绝成功");
    }

    /**
     * 退款订单列表
     * 1、默认返回最近10条退款订单
     * 2、搜索条件：
     * order_no 订单好
     * idname 借款人
     * create_time_start 下单时间范围，开始
     * create_time_end 下单时间范围，结束
     * approve_time_start 下单时间范围，开始
     * approve_time_end 下单时间范围，结束
     * approve_status 审批状态 0全部 1通过 2拒绝
     * page 第几页
     * rows 每页多少条记录
     * <p>
     * Created by yangchangyan on 2017/8/23.
     *
     * @param
     * @return
     * @throws
     */
    @RequestMapping(value = "refund/list")
    public void refundList() throws Exception {
        Map<String, Object> paramMap = ServletUtil.getParameterMap(request);

        if (String.valueOf(RefundOrderStatus.NO_REFUND_APPLY.getCode()).equals(paramMap.get("approve_status"))) {
            // 0，查找全部状态的订单
        } else {
            paramMap.put("refund_status", paramMap.get("approve_status"));
        }

        if (StringUtil.isNotEmpty(paramMap.get("create_time_start"))) {
            paramMap.put("create_time_gte", DateUtil.dateStringToTimestamp(paramMap.get("create_time_start").toString(), DateUtil.PATTERN_yyyy_MM_dd));
        }
        if (StringUtil.isNotEmpty(paramMap.get("create_time_end"))) {
            paramMap.put("create_time_lte", DateUtil.dateStringToTimestamp(paramMap.get("create_time_end").toString(), DateUtil.PATTERN_yyyy_MM_dd));
        }

        if (StringUtil.isNotEmpty(paramMap.get("approve_time_start"))) {
            paramMap.put("approve_refund_time_gte", DateUtil.dateStringToTimestamp(paramMap.get("approve_time_start").toString(), DateUtil.PATTERN_yyyy_MM_dd));
        }
        if (StringUtil.isNotEmpty(paramMap.get("approve_time_end"))) {
            paramMap.put("approve_refund_time_lte", DateUtil.dateStringToTimestamp(paramMap.get("approve_time_end").toString(), DateUtil.PATTERN_yyyy_MM_dd));
        }

        paramMap.put("sort", " create_time desc");
        paramMap.put("approve_refund_time_gte", "1"); //用户有申请退款的订单会在此字段记录操作时间

        List<RefundOrderModel> refundOrder = orderService.queryRefundOrder(paramMap, DbWREnums.READ);

        this.successData(FrameUtil.newHashMap(
                "maps", paramMap,
                "refundOrder", refundOrder));
    }

    /**
     * 退款处理
     * type 1 通过, 2 拒绝
     * <p>
     * Created by yangchangyan on 2017/8/23.
     *
     * @param
     * @return
     * @throws
     */
    @RequestMapping(value = "refund/process")
    public void refundProcess(String order_no, int type, String refuse_code, String content) throws Exception {
        if (StringUtil.isEmpty(order_no)) {
            this.errorMsg("无效的订单号");
            return;
        }

        if (type != RefundOrderStatus.REFUND_PASS.getCode() &&
                type != RefundOrderStatus.REFUND_REFUSE.getCode()) {
            this.errorMsg("无效操作");
            return;
        }

        OrderModel order = orderService.getModelOne(FrameUtil.newHashMap("order_no", order_no), DbWREnums.READ);
        if (null == order) {
            this.errorMsg("订单不存在");
            return;
        }

        // 登陆的商户与订单所属商户对应不上
        Long org_id = super.getMerchantId();
        if (order.getOrg_id().longValue() != org_id) {
            this.errorMsg("不能操作此订单");
            return;
        }

        OrderRefuseEnum orderRefuseEnum = null;

        if (type == RefundOrderStatus.REFUND_REFUSE.getCode()) {

            if (StringUtil.isEmpty(refuse_code)) {
                this.errorMsg("请选择拒绝码");
                return;
            }

            orderRefuseEnum = OrderRefuseEnum.parse(refuse_code);
            if (orderRefuseEnum == null) {
                this.errorMsg("您请求拒绝码不合法！");
                return;
            }

            if (OrderRefuseEnum.C.getCode().equals(orderRefuseEnum.getCode())) {
                if(StringUtil.isEmpty(content)){
                    this.errorMsg("请输入拒绝原因！");
                    return;
                }
            }
        }
        orderService.refundProcess(org_id, order.getOrder_id(), RefundOrderStatus.parse(type), orderRefuseEnum, content);
        this.successMsg("处理成功");
    }

    @Resource
    IOrderService orderService;

    @Resource
    private IFundChannelService fundChannelService;
}
